﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BitPortal.Models;
using BitPortal.Business;
using BitPortal.Utils;
using System.Text;
using System.Web.Security;

namespace BitPortal.Modules.Comments
{
    public partial class Comments : Controls.BaseUserControl
    {
        public DiscussionGroup DiscusionGroupReference { set; get; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindComments();
            }
        }
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            //BindComments();
        }

        protected void btnSendComments_OnClick(object sender, EventArgs e)
        {
            Discussion d = new Discussion();
            d.Name = txtName.Text;
            d.Email = txtEmail.Text;
            d.Avarta = "/Images/no_image.jpg";
            d.Content = taComment.Text;
            d.IsEnabled = !(bool)PortalConfigBLL.GetValueByName("Comment.ApproveBeforePost");
            d.IsDeleted = false;
            long ParentId = RespondTo.Value.ToInt64TryParse();
            d.IsReplyOf = (ParentId > 0) ? (long?)ParentId : null;
            d.LanguageCode = BitContext.Current.Lang.LanguageCode;
            d.Logs.DatetimeCreated = d.Logs.DatetimeEdited = DateTime.Now;
            d.Logs.CreatingUser = d.Logs.EditingUser = BitContext.Current.LoginUser;

            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            DiscussionGroup dg = bitEnt.DiscussionGroups.SingleOrDefault(ent => ent.DiscussionGroupID == this.DiscusionGroupReference.DiscussionGroupID);
            dg.DiscussionCount++;
            dg.Discussions.Add(d);
            if (bitEnt.SaveChanges() > 0)
            {
                taComment.Text = "";
                if ((bool)PortalConfigBLL.GetValueByName("Comment.ApproveBeforePost"))
                {
                    (this.Page as Controls.BaseFrontendPage).ShowArlert(Resources.BitResources.CommentSuccessAndWait);
                }
                else
                {
                    string fn = "AddComment(" + RespondTo.Value + "," + d.DiscussionID + ",'" + d.Content + "','" + d.Avarta + "','" + d.Name + "','" + d.Logs.DatetimeCreated + "')";
                    (this.Page as Controls.BaseFrontendPage).ShowArlert(message: Resources.BitResources.CommentSussess, callback: "function(){" + fn + "}");
                    //ScriptManager.RegisterStartupScript(this.Page, typeof(Page), "AppendComment", "AddComment(" + RespondTo.Value + "," + d.DiscussionID + ",'" + d.Content + "','" + d.Avarta + "','" + d.Name + "','" + d.Logs.DatetimeCreated + "')", true);
                    //BindComments();
                }
            }
        }

        public void BindComments()
        {
            ltrHeader.Text = "<a href=\"javascript:OpenComment(0)\" >" + Resources.BitResources.Comments + "</a> (" + DiscussionBLL.GetByCondition(BitContext.Current.Lang.LanguageCode, DiscusionGroupReference.DiscussionGroupID, -1, true).Count() + ") ";
            StringBuilder str = new StringBuilder();
            str.Append("<ul id=\"Comments_0\">");
            foreach (Discussion d in DiscussionBLL.GetByCondition(BitContext.Current.Lang.LanguageCode, DiscusionGroupReference.DiscussionGroupID, null, true).ToList())
            {
                BindComments(d, str);
            }
            str.Append("</ul>");
            ltrComments.Text = str.ToString();
        }

        private void BindComments(Discussion parent, StringBuilder str)
        {
            str.Append("<li>");
            str.Append("<div class=\"Comment-Item\">");
            str.Append("<div class=\"Avatar\"> <img src=\"/Images/no_image.jpg\" width=\"60\" height=\"60\" /></div>");
            str.Append("<div class=\"Detail-Wrapper\">");
            str.Append("<div class=\"Detail\"> ");
            str.Append("<p class=\"Content\">" + parent.Content + "</p>");
            str.Append("<span class=\"Author\">" + parent.Name + "</span> ");
            str.Append("<span class=\"date\">" + (parent.Logs.DatetimeCreated).ToString() + "</span>");
            if ((bool)PortalConfigBLL.GetValueByName("Comment.AllowReply"))
            {
                str.Append("<div class=\"reply\">");
                str.Append("<a onclick=\"OpenComment(" + parent.DiscussionID + ")\">Reply</a>");
                str.Append("</div>");
            }
            str.Append("</div></div></div>");

            List<Discussion> listChilds = DiscussionBLL.GetByCondition(BitContext.Current.Lang.LanguageCode, DiscusionGroupReference.DiscussionGroupID, parent.DiscussionID, true).ToList();
            if (listChilds.Count > 0 || true)
            {
                str.Append("<ul class=\"Replies\" id=\"Comments_" + parent.DiscussionID + "\">  ");
                foreach (Discussion d in listChilds)
                {
                    BindComments(d, str);
                }
                str.Append("</ul>");
            }
            str.Append("</li>");
        }
    }
}